package org.spark.java.transformation;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 功能描述: 根据key分组 也就是key value 形式
 * Tuple2对象类似于java里面的map
 *
 * @param:
 * @return:
 * @auther: 某人的目光
 * @date: 2020/1/12 23:13
 */
public class GroupByKey {
    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf();
        //设置运行环境
        sparkConf.setMaster("local");
        sparkConf.setAppName("groupByKey");
        JavaSparkContext context = new JavaSparkContext(sparkConf);
        List<Tuple2<String, String>> list = Arrays.asList(
                new Tuple2<String, String>("江苏", "南京"),
                new Tuple2<String, String>("安徽", "阜阳"),
                new Tuple2<String, String>("安徽", "颍上县"));
        JavaPairRDD<String, String> listRdd = context.parallelizePairs(list);

        JavaPairRDD<String, Iterable<String>> groupByKey = listRdd.groupByKey();

        groupByKey.foreach(new VoidFunction<Tuple2<String, Iterable<String>>>() {
            public void call(Tuple2<String, Iterable<String>> t) throws Exception {
                System.out.println(t._1);
                Iterator<String> iterable = t._2.iterator();
                while (iterable.hasNext()) {
                    System.out.println(iterable.next());
                }
                System.out.println("==========================");
            }
        });
    }
}
